﻿using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;

namespace com.uplus.log4net
{ 
    public static class Log4Helper
    {
        #region 定义变量
        static ILoggerRepository Repository { get; set; }
        #endregion

        #region 初始化
        /// <summary>
        /// 初始化log4net，输出到log日志文件
        /// </summary>
        public static void Initializer(string fileName="")
        {
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = "log4net.config";
            }
            ILoggerRepository repository = LogManager.CreateRepository(Assembly.GetCallingAssembly().FullName);
            XmlConfigurator.Configure(repository, new FileInfo(fileName));
            Repository = repository;
        }
        /// <summary>
        /// 初始化log4net，以控制台形式输出
        /// </summary>
        public static void InitailizerConsole()
        {
            ILoggerRepository repository = LogManager.CreateRepository(Assembly.GetCallingAssembly().FullName);
            BasicConfigurator.Configure(repository);
            Repository = repository;
        }
        #endregion

        #region Fatal
        public static void Fatal(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name,type);
            if (exception == null)
                log.Fatal(message);
            else
                log.Fatal(message, exception);
        }
        #endregion

        #region Error
        public static void Error(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name,type);
            if (exception == null)
                log.Error(message);
            else
                log.Error(message, exception);
        }
        public static void Error(string name, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name, name);
            if (exception == null)
                log.Error(message);
            else
                log.Error(message, exception);
        }
        #endregion

        #region Warn
        public static void Warn(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name,type);
            if (exception == null)
                log.Warn(message);
            else
                log.Warn(message, exception);
        }

        public static void Error(string v1, string v2, string message)
        {
            throw new NotImplementedException();
        }

        public static void Warn(string name, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name, name);
            if (exception == null)
                log.Warn(message);
            else
                log.Warn(message, exception);
        }
        #endregion

        #region Info
        public static void Info(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name,type);
            if (exception == null)
                log.Info(message);
            else
                log.Info(message, exception);
        }

        public static void Info(string name, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name, name);
            if (exception == null)
                log.Info(message);
            else
                log.Info(message, exception);
        }
        #endregion

        #region Debug
        public static void Debug(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name,type);
            if (exception == null)
                log.Debug(message);
            else
                log.Debug(message, exception);
        }

        public static void Debug(string name, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(Repository.Name, name);
            if (exception == null)
                log.Debug(message);
            else
                log.Debug(message, exception);
        }

        public static void info(Type type, string v)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}